package com.hd.tiyu.yuejian.Fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.hd.tiyu.yuejian.Adapter.WenTiAdapter;
import com.hd.tiyu.yuejian.JavaBean.Tiwen;
import com.hd.tiyu.yuejian.Model.ListsModel;
import com.hd.tiyu.yuejian.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/10/9.
 */

public class WentiFragment extends Fragment {
    private View view;
    private String id;
    private Button tiwen;
    private EditText wenti;
    private RecyclerView recyclerView;
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.wentitab,container,false);
        Bundle bundle=getArguments();
        id=bundle.getString("id");
        initView();
        initData();
        return view;
    }

    private void initView() {
        tiwen=view.findViewById(R.id.master_tiwen);
        wenti=view.findViewById(R.id.masteredit_tiwen);
        recyclerView=view.findViewById(R.id.re_tiwen_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    private void initEvent() {
        tiwen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String master_id="1";
                SharedPreferences mSharedPreferences=getActivity().getSharedPreferences("loginUser", Context.MODE_PRIVATE);
                String user_id=mSharedPreferences.getString("user_id","");
                String question=wenti.getText().toString();
                ListsModel model=new ListsModel();
                Call<String> call=model.putWenti(user_id,master_id,question);
                call.enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        String id=response.body();

                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {

                    }
                });
            }
        });
    }

    private void initData() {
        ListsModel model=new ListsModel();
        Call<List<Tiwen>>call=model.getlistsTiWen(id);
        call.enqueue(new Callback<List<Tiwen>>() {
            @Override
            public void onResponse(Call<List<Tiwen>> call, Response<List<Tiwen>> response) {
                List<Tiwen>list =response.body();
                recyclerView.setAdapter(new WenTiAdapter(getActivity(),list));
            }

            @Override
            public void onFailure(Call<List<Tiwen>> call, Throwable t) {

            }
        });
    }
}
